package com.sitc.administrator.application.activity;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.sitc.administrator.application.R;

import java.io.File;
import java.math.BigDecimal;

import static com.sitc.administrator.application.R.*;
import static com.sitc.administrator.application.util.ToastUtil.showSortToast;


public class baiduMapActivity extends BaseTitleActivity {

    public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
    public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名
    public static final String PN_TENCENT_MAP = "com.tencent.map"; // 腾讯地图包名

    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;

    private LatLng destination = null;
    private LatLng latlng = null;

    public static boolean isGdMapInstalled(){
        return isInstallPackage(PN_GAODE_MAP);
    }

    public static boolean isBaiduMapInstalled(){
        return isInstallPackage(PN_BAIDU_MAP);
    }

    public static boolean isTencentMapInstalled(){
        return isInstallPackage(PN_TENCENT_MAP);
    }


    private static boolean isInstallPackage(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        onTheme();
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_baidu_map);

    }

    @Override
    protected void initViews() {
        super.initViews();
        enableBackMenu();
        setTitle(getResources().getString(string.home_stations_location));


        //获取地图控件引用
        mMapView = (MapView) findViewById(id.bmapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);




    }


    @Override
    protected void initDatas() {
        super.initDatas();

        latlng= new LatLng(29.521696, 106.667499);

        destination = convertBaiduToGPS(latlng);

        MapStatus mMapStatus = new MapStatus.Builder()
                .target(latlng)
                .zoom(18)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);

        Button button = new Button(this);
        button.setText("xx站点");
        button.setBackgroundResource(R.drawable.popup);


        //在地图上显示文字覆盖物
        //Overlay mText = mBaiduMap.addOverlay(mTextOptions);

        BitmapDescriptor mBitmap = BitmapDescriptorFactory.fromView(button);

        //响应点击的OnInfoWindowClickListener
        InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick() {
                showCustomizeDialog();
            }
        };

        //构造InfoWindow
        //point 描述的位置点
        //-100 InfoWindow相对于point在y轴的偏移量
        InfoWindow mInfoWindow = new InfoWindow(mBitmap, latlng, -100, listener);

        //使InfoWindow生效
        mBaiduMap.showInfoWindow(mInfoWindow);




    }





    public static void openBaiDuMap(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
        String uriString = null;
        StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");




        if (slat != 0 && slon != 0){

            builder.append("origin=latlng:")
                    .append(slat)
                    .append(",")
                    .append(slon)
                    .append("|name:")
                    .append(sname);
        }

        builder.append("&destination=latlng:")
                .append(dlat)
                .append(",")
                .append(dlon)
                .append("|name:")
                .append(dname);
        uriString = builder.toString();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_BAIDU_MAP);
        intent.setData(Uri.parse(uriString));
        context.startActivity(intent);
    }



    public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
        String uriString = null;
        StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");

        if (slat != 0 && slon != 0) {
            builder.append("&sname=").append(sname)
                    .append("&slat=").append(slat)
                    .append("&slon=").append(slon);
        }

        builder.append("&to=").append(dname)
                .append("&tocoord=").append(dlat)
                .append(",")
                .append(dlon);
        uriString = builder.toString();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_TENCENT_MAP);
        intent.setData(Uri.parse(uriString));
        context.startActivity(intent);
    }


    public static void openGaoDeMap(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
        String uriString = null;
        StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
        if (slat != 0 && slon != 0) {
            builder.append("&sname=").append(sname)
                    .append("&slat=").append(slat)
                    .append("&slon=").append(slon);
        }
        builder.append("&dlat=").append(dlat)
                .append("&dlon=").append(dlon)
                .append("&dname=").append(dname)
                .append("&dev=0")
                .append("&t=0");
        uriString = builder.toString();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_GAODE_MAP);
        intent.setData(Uri.parse(uriString));
        context.startActivity(intent);
    }

    public static LatLng convertBaiduToGPS(LatLng latlng){

        return new CoordinateConverter().from(CoordinateConverter.CoordType.BD09LL).coord(latlng).convert();
    }



    private void showCustomizeDialog() {

        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(this);
        final View dialogView = LayoutInflater.from(this)
                .inflate(layout.map_dialog_layout,null);
        dialogView.findViewById(R.id.baidu).setOnClickListener(this);
        dialogView.findViewById(id.gaode).setOnClickListener(this);
        dialogView.findViewById(id.tenxun).setOnClickListener(this);

        customizeDialog.setTitle("导航地图选择");
        customizeDialog.setView(dialogView);
        customizeDialog.show();
    }



    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.baidu){

            if(isBaiduMapInstalled()&&destination !=null){
                openBaiDuMap(getBaseContext(),0,0,"",
                        latlng.latitude,latlng.longitude,"城市便捷酒店");

            }else {showSortToast(getBaseContext(),"百度地图没有安装！");}

        }
        if(v.getId() == R.id.tenxun){

            if(isTencentMapInstalled()&&destination !=null){
                openTencentMap(getBaseContext(),0,0,"",
                        destination.latitude,destination.longitude,"城市便捷酒店");
            }else {showSortToast(getBaseContext(),"腾讯地图没有安装！");}

        }
        if(v.getId() == R.id.gaode){

            if(isGdMapInstalled()&&destination !=null){
                openGaoDeMap(getBaseContext(),0,0,"",
                        destination.latitude,destination.longitude,"城市便捷酒店");
            }else {showSortToast(getBaseContext(),"高德地图没有安装！");}

        }

    }



    @Override
    protected void initListener() {
        super.initListener();

    }


    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }


}
